import java.util.*;

public class QuickT9 {

	public int minimumPressings(String[] t9, String word) {
		Set<String> prefixes = new HashSet<String>();
		prefixes.add("");
		for (String row : t9) {
			String[] words = row.split(" ");
			for (String dictionaryWord : words) {
				for (int i = 1; i <= dictionaryWord.length(); i++)
					prefixes.add(dictionaryWord.substring(0, i));
			}
		}
		Map<String, List<String>> digitCombos = new HashMap<String, List<String>>();
		Map<String, String> digitsByPrefix = new HashMap<String, String>();
		for (String prefix : prefixes) {
			String key = convert(prefix);
			List<String> value = digitCombos.get(key);
			if (value == null)
				digitCombos.put(key, value = new ArrayList<String>());
			value.add(prefix);
			digitsByPrefix.put(prefix, key);
		}
		Map<String, String> next = new HashMap<String, String>();
		for (List<String> value : digitCombos.values()) {
			Collections.sort(value);
			for (int i = 1; i < value.size(); i++)
				next.put(value.get(i - 1), value.get(i));
			next.put(value.get(value.size() - 1), value.get(0));
		}
		Map<String, Integer> result = new HashMap<String, Integer>();
		Map<String, Integer> finalResult = new HashMap<String, Integer>();
		result.put("", 0);
		Queue<String> queue = new LinkedList<String>();
		queue.add("");
		while (!queue.isEmpty()) {
			String current = queue.poll();
			int currentResult = result.get(current);
			String digits = digitsByPrefix.get(current);
			for (char c = '2'; c <= '9'; c++) {
				String newDigits = digits + c;
				if (digitCombos.get(newDigits) != null)
					tryAdd(queue, result, digitCombos.get(newDigits).get(0), currentResult, finalResult);
			}
			tryAdd(queue, result, next.get(current), currentResult, finalResult);
			for (int i = current.length() - 1; i > 0; i--) {
				String key = current.substring(0, i);
				int value = currentResult + current.length() - i;
				if (!finalResult.containsKey(key) || finalResult.get(key) > value)
					finalResult.put(key, value);
			}
		}
		int[] minStrokes = new int[word.length() + 1];
		Arrays.fill(minStrokes, Integer.MAX_VALUE);
		minStrokes[0] = 0;
		for (int i = 0; i < word.length(); i++) {
			if (minStrokes[i] == Integer.MAX_VALUE)
				continue;
			for (int j = i + 1; j <= word.length() && prefixes.contains(word.substring(i, j)); j++)
				minStrokes[j] = Math.min(minStrokes[j], minStrokes[i] + finalResult.get(word.substring(i, j)));
		}
		return minStrokes[word.length()] == Integer.MAX_VALUE ? -1 : minStrokes[word.length()];
	}

	private void tryAdd(Queue<String> queue, Map<String, Integer> result, String candidate, int currentResult,
			Map<String, Integer> finalResult) {
		if (result.get(candidate) == null) {
			result.put(candidate, currentResult + 1);
			if (!finalResult.containsKey(candidate) || finalResult.get(candidate) > currentResult + 2)
				finalResult.put(candidate, currentResult + 2);
			queue.add(candidate);
		}
	}

	private String convert(String prefix) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < prefix.length(); i++) {
			switch (prefix.charAt(i)) {
			case 'a': case 'b': case 'c':
				builder.append('2');
				break;
			case 'd': case 'e': case 'f':
				builder.append('3');
				break;
			case 'g': case 'h': case 'i':
				builder.append('4');
				break;
			case 'j': case 'k': case 'l':
				builder.append('5');
				break;
			case 'm': case 'n': case 'o':
				builder.append('6');
				break;
			case 'p': case 'q': case 'r': case 's':
				builder.append('7');
				break;
			case 't': case 'u': case 'v':
				builder.append('8');
				break;
			case 'w': case 'x': case 'y': case 'z':
				builder.append('9');
				break;
			default:
				throw new RuntimeException("shit happens");
			}
		}
		return builder.toString();
	}

	// BEGIN CUT HERE
   public static void main(String[] args) {
		if ( args.length == 0 ) {
			QuickT9Harness.run_test(-1);
		} else {
			for ( int i=0; i<args.length; ++i )
				QuickT9Harness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class QuickT9Harness {
	public static void run_test( int casenum ) {
		if ( casenum != -1 ) {
			if ( runTestCase( casenum ) == -1 )
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for ( int i=0;; ++i ) {
			int x = runTestCase(i);
			if ( x == -1 ) {
				if ( i >= 100 ) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if ( total == 0 ) {
			System.err.println("No test cases run.");
		} else if ( correct < total ) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(int expected, int result) { return expected == result; }
	static String formatResult(int res) {
		return String.format("%d", res);
	}
	
	static int verifyCase( int casenum, int expected, int received ) { 
		System.err.print("Example " + casenum + "... ");
		if ( compareOutput( expected, received ) ) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
			return 0;
		}
	}

	static int runTestCase( int casenum ) {
		switch( casenum ) {
		case 0: {
			String[] t9               = {"aae", "bab", "abad", "bdbd", "beta"};
			String word               = "babe";
			int expected__            = 9;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}
		case 1: {
			String[] t9               = {"ann","ie"};
			String word               = "annie";
			int expected__            = 7;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}
		case 2: {
			String[] t9               = {"ann","amm"};
			String word               = "annie";
			int expected__            = -1;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}
		case 3: {
			String[] t9               = {"aaa aab","aac aba abb ccca"};
			String word               = "aba";
			int expected__            = 6;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}
		case 4: {
			String[] t9               = {"acac aba aaab","aab aa baa","bba bacade abb","baba"};
			String word               = "abbaca";
			int expected__            = 10;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}
		case 5: {
			String[] t9               = {"aaa aab aac","aba abb","ccca"};
			String word               = "ccc";
			int expected__            = 5;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}

		// custom cases

/*      case 6: {
			String[] t9               = ;
			String word               = ;
			int expected__            = ;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}*/
/*      case 7: {
			String[] t9               = ;
			String word               = ;
			int expected__            = ;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}*/
/*      case 8: {
			String[] t9               = ;
			String word               = ;
			int expected__            = ;

			return verifyCase( casenum, expected__, new QuickT9().minimumPressings( t9, word ) );
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE
